{{- if (hasKey .Values.prometheus.internal.alertmanagers "internal") }}
  {{- range .Values.prometheus.internal.alertmanagers.internal }}
---
apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
metadata:
  name: {{ .name }}
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list $ (dict "app" "alertmanager")) | nindent 2 }}
spec:
  alertmanagerConfiguration:
    name: {{ .name }}
  replicas: {{ include "helm_lib_is_ha_to_value" (list $ 2 1) }}
  image: {{ include "helm_lib_module_image" (list $ "alertmanager") }}
  imagePullSecrets:
  - name: deckhouse-registry
  listenLocal: true
  serviceAccountName: alertmanager-internal
  volumes:
  - name: kube-rbac-proxy-ca
    configMap:
      defaultMode: 420
      name: kube-rbac-proxy-ca.crt
  containers:
  - name: kube-rbac-proxy
    {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" $ | nindent 4 }}
    image: {{ include "helm_lib_module_common_image" (list $ "kubeRbacProxy") }}
    args:
    - "--secure-listen-address=$(KUBE_RBAC_PROXY_LISTEN_ADDRESS):9093"
    - "--client-ca-file=/etc/kube-rbac-proxy/ca.crt"
    - "--v=2"
    - "--logtostderr=true"
    - "--stale-cache-interval=1h30m"
    ports:
    - containerPort: 9093
      name: https
    env:
    - name: KUBE_RBAC_PROXY_LISTEN_ADDRESS
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
    - name: KUBE_RBAC_PROXY_CONFIG
      value: |
        upstreams:
        - upstream: http://127.0.0.1:9093/
          path: /
          authorization:
            resourceAttributes:
              namespace: d8-monitoring
              apiGroup: monitoring.coreos.com
              apiVersion: v1
              resource: prometheuses
              subresource: http
              name: main
    volumeMounts:
    - name: kube-rbac-proxy-ca
      mountPath: /etc/kube-rbac-proxy
    resources:
      requests:
        {{- include "helm_lib_module_ephemeral_storage_only_logs" $ | nindent 8 }}
  {{- if not ( $.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
        {{- include "helm_lib_container_kube_rbac_proxy_resources" . | nindent 8 }}
  {{- end }}
  {{- if (include "helm_lib_ha_enabled" $) }}
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app.kubernetes.io/name: alertmanager
            alertmanager: {{ .name }}
        topologyKey: kubernetes.io/hostname
  {{- end }}
{{- if $.Values.global.modules.publicDomainTemplate }}
  externalUrl: {{ include "helm_lib_module_uri_scheme" $ }}://{{ include "helm_lib_module_public_domain" (list $ "grafana") }}/alertmanager/{{ .name }}
{{- end }}
  {{- include "helm_lib_module_pod_security_context_run_as_user_deckhouse" $ | nindent 2 }}
  {{- include "helm_lib_node_selector" (tuple $ "monitoring") | nindent 2 }}
  {{- include "helm_lib_tolerations" (tuple $ "monitoring") | nindent 2 }}
  {{- include "helm_lib_priority_class" (tuple $ "cluster-low") | nindent 2 }}
  resources:
    requests:
      {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 50 | nindent 6 }}
    {{- if not ( $.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
      {{- include "alertmanager_resources" . | nindent 6 }}
    {{- end }}
  {{- end }}
{{- end }}
